<?php

/**
 * FaqController is the default controller for this application
 */
class FaqController extends ControllerBase {
	/**
	 * FaqDataService Object
	 *
	 * @var FaqDataService object
	 */
	public $faqDataService = null;
	
	/**
	 * FaqCategoryDataService Object
	 *
	 * @var FaqCategoryDataService object
	 */
	public $faqCategoryDataService = null;
	
	public function __construct() {
		parent::__construct ();
		
		// Init Authentication class
		$authentication = new Authentication ();
		
		// Only administrator can right
		if ($authentication->isAdministrator () == false) {
			
			// Go to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Login/index';
			CommonUtils::redirect ( $url );
		}
		
		// Get status list of product reviews
		$this->smarty->assign ( 'statusList', FaqEntity::getStatusList () );
		
		// New Product Reviews Data Service
		$this->faqDataService = new FaqDataService ();
		
		// Faq Category DataService
		$this->faqCategoryDataService = new FaqCategoryDataService ();
		
		// Set page name
		$this->setPageName ( 'faq' );
	
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site. Return list of all product category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function indexAction() {
		// View product_comment list
		$this->listAction ();
	}
	
	/**
	 * Dispplay List of product_comment
	 * landing page of the site. Return list of all product_comment
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function listAction() {
		
		// Get all items of product category
		$arrElement = array ();
		
		$faqCategoryId = $this->getQuery ( 'id', "" );
		if ($faqCategoryId != "") {
			$arrElement ['id'] = $faqCategoryId;
		}
		
		$faqEntities = $this->faqDataService->getItems ( $arrElement, " createdDate DESC", NULL, NULL );
		
		$this->smarty->assign ( 'itemList', $faqEntities );
		$this->smarty->assign ( 'itemListCount', sizeof ( $faqEntities, false ) );
		$this->smarty->assign ( 'i', 0 );
		$this->smarty->assign ( 'this', $this );
		$_SESSION ['tokenKey'] = sha1 ( CommonUtils::randomNumber ( 10 ) );
		$this->smarty->assign ( 'actionKey', $_SESSION ['tokenKey'] );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Faq/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Active some records of product_comment
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function activeAction() {
		$this->updateStatus ( FaqEntity::ACTIVE );
		
		// View product_comment list
		$this->listAction ();
	}
	
	/**
	 * Inactive some records of product_comment
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function inactiveAction() {
		$this->updateStatus ( FaqEntity::INACTIVE );
		
		// View product_comment list
		$this->listAction ();
	}
	
	/**
	 * Delete action is trigger when administrator want to delete a product_comment
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteAction() {
		// Get action key
		$actionKey = $this->getPost ( 'actionKey', '' );
		$id = $this->getPost ( 'idValue', '' );
		
		// Delete a product_comment item
		$res = $this->faqDataService->del ( $id );
		
		// Go to login page
		$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Faq/index';
		CommonUtils::redirect ( $url );
	}
	
	/**
	 * Delete list of product_comment
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteallAction() {
		$iCount = $this->getPost ( 'iCount', 0 );
		for($i = 0; $i < $iCount; $i ++) {
			$id = $this->getPost ( 'checkItem' . $i, '' );
			if (! empty ( $id )) {
				
				// Delete a product_comment item
				$this->faqDataService->del ( $id );
			}
		}
		
		// View product_comment list
		$this->listAction ();
	}
	
	/**
	 * Update status list of product_comment
	 *
	 * @access private
	 *
	 * @param int $status: Status of the product_comment
	 *
	 * @return void
	 */
	protected function updateStatus($status) {
		$iCount = $this->getPost ( 'iCount', 0 );
		for($i = 0; $i < $iCount; $i ++) {
			$id = $this->getPost ( 'checkItem' . $i, '' );
			
			if (! empty ( $id )) {
				$this->faqDataService->updateStatus ( $id, $status );
			}
		}
	
	}
	
	/**
	 * Edit action is trigger when admin want to edit the forum_topic information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function editAction() {
		
		$authentication = new Authentication ();
		// Error flag
		$isError = false;
		$error = "";
		
		// Get id of forum_topic
		$id = $this->getPost ( 'id', '' );
		if (is_null ( $id ) || $id == '') {
			$id = $this->getQuery ( 'id', '' );
		}
		// If the id of forum_topic does not exist, go on to view list page
		if (empty ( $id )) {
			$this->listAction ();
			return - 1;
		}
		
		// Get action key
		$actionKey = $this->getPost ( 'actionKey', '' );
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			// Get name field
			$question = $this->getPost ( 'question', '' );
			
			// get Answer
			$answer = $this->getPost ( 'answer', "" );
			
			// remove magic quotes
			if (get_magic_quotes_gpc ()) {
				$answer = stripslashes ( $answer );
			}
			
			// Get FAQ CategoryID
			$faqCategoryId = $this->getPost ( 'faqCategoryId', '' );
			
			// Get status field
			$status = $this->getPost ( 'status', '' );
			
			try {
				// Save data into database
				$faqEntity = $this->faqDataService->beforeUpdate ( $id );
				$faqEntity->id = $id;
				$faqEntity->categoryId = $faqCategoryId;
				$faqEntity->question = $question;
				$faqEntity->answer = $answer;
				$faqEntity->status = $status;
				$res = $this->faqDataService->update ( $faqEntity );
				
				// Show a message
				$error = 'The record has been updated successfully';
			} catch ( Exception $ex ) {
				
				echo $ex->getMessage ();
				// Ignore to process this case. Only show a message
				$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
				$isError = true;
			}
		}
		
		// View data
		$faqEntityDetail = $this->faqDataService->getById ( $id );
		
		// If the id of forum_topic not exist, go on to view list page
		if (is_null ( $faqEntityDetail ) || is_null ( $faqEntityDetail )) {
			$this->listAction ();
			return - 1;
		}
		
		// Error Handling
		$this->smarty->assign ( 'isError', $isError );
		$categoriesList = $this->faqCategoryDataService->getCategoryList ();
		
		$this->smarty->assign ( 'arrCatList', $categoriesList );
		
		// Create new token key
		$_SESSION ['tokenKey'] = sha1 ( CommonUtils::randomNumber ( 10 ) );
		$this->smarty->assign ( 'error', isset ( $error ) ? $error : '' );
		$this->smarty->assign ( 'actionKey', $_SESSION ['tokenKey'] );
		$this->smarty->assign ( 'id', ($isError == true) ? $id : $faqEntityDetail->id );
		$this->smarty->assign ( 'question', ($isError == true) ? $question : $faqEntityDetail->question );
		$this->smarty->assign ( 'answer', ($isError == true) ? $answer : $faqEntityDetail->answer );
		$this->smarty->assign ( 'status', ($isError == true) ? $status : $faqEntityDetail->status );
		$this->smarty->assign ( 'faqCategoryId', ($isError == true) ? $faqCategoryId : $faqEntityDetail->categoryId );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Faq/edit.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	public function addAction() {
		
		$authentication = new Authentication ();
		// Error flag
		$isError = false;
		$error = "";
		
		// Get action key
		$actionKey = $this->getPost ( 'actionKey', '' );
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			// Get name field
			$question = $this->getPost ( 'question', '' );
			
			// get Answer
			$answer = $this->getPost ( 'answer', "" );
			
			// remove magic quotes
			if (get_magic_quotes_gpc ()) {
				$answer = stripslashes ( $answer );
			}
			
			// Get FAQ CategoryID
			$faqCategoryId = $this->getPost ( 'faqCategoryId', '' );
			
			// Get status field
			$status = $this->getPost ( 'status', '' );
			try {
				// Save data into database
				$faqEntity = $this->faqDataService->beforeCreate ();
				$faqEntity->categoryId = $faqCategoryId;
				$faqEntity->question = $question;
				$faqEntity->answer = $answer;
				$faqEntity->status = $status;
				$res = $this->faqDataService->create ( $faqEntity );
				
				// Show a message
				$error = 'The record has been updated successfully';
			} catch ( Exception $ex ) {
				
				echo $ex->getMessage ();
				// Ignore to process this case. Only show a message
				$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
				$isError = true;
			}
		}
		
		// Error Handling
		$this->smarty->assign ( 'isError', $isError );
		$arrElement = array ();
		$arrElement ["status"] = 1;
		$categoriesList = $this->faqCategoryDataService->getCategoryList ();
		$this->smarty->assign ( 'arrCatList', $categoriesList );
		
		// Create new token key
		$_SESSION ['tokenKey'] = sha1 ( CommonUtils::randomNumber ( 10 ) );
		$this->smarty->assign ( 'question', isset ( $question ) ? $question : "" );
		$this->smarty->assign ( 'answer', isset ( $answer ) ? $answer : "" );
		$this->smarty->assign ( 'faqCategoryId', isset ( $faqCategoryId ) ? $faqCategoryId : "" );
		$this->smarty->assign ( 'status', isset ( $status ) ? $status : "" );
		$this->smarty->assign ( 'error', isset ( $error ) ? $error : '' );
		$this->smarty->assign ( 'actionKey', $_SESSION ['tokenKey'] );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Faq/add.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}

}
